package cn.itcast.itcaststore.web.servlet.manager;

import cn.itcast.itcaststore.domain.Notice;
import cn.itcast.itcaststore.serive.NoticeService;
import cn.itcast.itcaststore.serive.impl.NoticeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@WebServlet("/admin/EditNoticeServlet")
public class EditNoticeServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doPost(req, resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        NoticeService nService = new NoticeServiceImpl();
        //获取表单参数
        Map<String, String[]> noticeMap = req.getParameterMap();
        Notice notice = new Notice();
        try {
            BeanUtils.populate(notice,noticeMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        notice.setN_time(date);
        //调用service层
        try {
            nService.updateNotice(notice);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        resp.sendRedirect(req.getContextPath() + "/admin/findNotice");
    }
}
